<?php
namespace app\wechat\library;
use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
use think\Log;
use app\common\model\User;
use app\common\model\Account;
class WechatEvent implements EventHandlerInterface{
    protected $message;
    public function handle($payload = null){
        Log::info($payload);
        switch ($payload['Event']) {
            case 'subscribe':
                if(isset($payload['EventKey']) && $payload['EventKey']){
                    //验证账户
                    $this->verifyAccount($payload['FromUserName'],str_replace('SCENE_','',$payload['EventKey']));
                    return $this->message;
                }
                break;
            
            case 'SCAN':
                if(isset($payload['EventKey']) && $payload['EventKey']){
                    //验证账户
                    $this->verifyAccount($payload['FromUserName'],$payload['EventKey']);
                    return $this->message;
                }
                break;
        }
        return null;
    }
    //检测账户
    protected function verifyAccount($openid,$device_id){
        $user = User::get(['openid'=>$openid]);
        if(empty($user)){
            //用户没有注册
            $this->message = '';
            return false;
        }
        if($user['is_verify'] != '1'){
            //用户没有注册
            return false;
        }
        //判断余额
        $account = Account::get(['id'=>$user['account_id']]);
        if(empty($account)){
            //没有账户
            $this->message = '';
            return false;
        }
        if(($account['balance']+$account['extra_amount']) < 8){
            //余额不足充值
            $this->message = '';
            return false;
        }
        //创建订单启动机器
    }

}